import {defineStore} from 'pinia'
import type {RouteLocationNormalizedLoaded} from 'vue-router'
import {findMenuByUrl} from "@/utils/tabs";
import {getRoutePath} from "@/utils/tool";

export const tabsStore = defineStore('tabsStore', {
    state: () => ({
        visitedViews: [] as any[],
        cachedViews: [] as any[],
        delTabName: "" as string, //当前关闭的TabName
    }),
    actions: {
        addView(view: RouteLocationNormalizedLoaded) {

            //不显示在Tab中
            if (view.meta && view.meta.notInTab) {
                return;
            }

            if (this.visitedViews.some(function (value) {
                return value.path === view.fullPath;
            })) {
                return
            }
            //如果地址中带了?或 :/ 则到路表由中再查找，因为可以是路由复用
            if (view.fullPath.includes("?") || view.meta && view.meta.multiplex) {
                const find = findMenuByUrl(view.fullPath);
                if (find) {
                    view.meta.title = find.meta.title;
                    view.meta.id = find.meta.id;
                    view.meta.url = find.meta.url;
                }
            }

            this.visitedViews.push(
                Object.assign({}, view, {
                    title: view.meta?.title || 'unknown',
                    path: view.fullPath
                })
            )
        },
        addCachedView(view: RouteLocationNormalizedLoaded) {

            const viewName = getRoutePath(view);
            //view.name
            if (this.cachedViews.includes(viewName)) {
                return
            }

            if (view.meta.cache) {
                this.cachedViews.push(viewName)
            }
        },
        delView(view: RouteLocationNormalizedLoaded) {
            for (const [i, v] of this.visitedViews.entries()) {
                //if (v.path === view.path) {
                if (v.meta.url === view.meta.url) {
                    this.visitedViews.splice(i, 1)
                    break
                }
            }

            this.delTabName = getRoutePath(view);

            this.delCachedView(view).then()
        },
        delCachedView(view: RouteLocationNormalizedLoaded) {
            return new Promise(resolve => {
                const viewName = getRoutePath(view);
                //view.name
                const index = this.cachedViews.indexOf(viewName)
                if (index > -1) {
                    this.cachedViews.splice(index, 1)
                }
                resolve([...this.cachedViews])
            })
        },
        delOthersViews(view: RouteLocationNormalizedLoaded) {
            this.visitedViews = this.visitedViews.filter(v => {
                return v.meta.affix || v.path === view.path
            })

            const index = this.cachedViews.indexOf(view.name)
            if (index > -1) {
                this.cachedViews = this.cachedViews.slice(index, index + 1)
            } else {
                this.cachedViews = []
            }
        },
        delAllViews() {
            this.visitedViews = this.visitedViews.filter(tab => tab.meta.affix)
            this.cachedViews = []
        }
    }
})
